package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.valid.AddGroup;
import com.sky.valid.UpdateGroup;
import com.sky.vo.SetmealVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * TODO 找时间补完swagger
 */
@RestController("adminSetmealController")
@RequestMapping("/admin/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    /**
     * 新增套餐
     * @param dto
     * @return
     */
    @PostMapping
    public Result save(@RequestBody @Validated(AddGroup.class) SetmealDTO dto){
        setmealService.save(dto);
        return Result.success();
    }

    /**
     * 套餐分页查询
     * @param dto
     * @return
     */
    @GetMapping("/page")
    public Result page(@Validated SetmealPageQueryDTO dto){
        PageResult pageResult = setmealService.page(dto);
        return Result.success(pageResult);
    }

    /**
     * 删除套餐
     * @param ids
     * @return
     */
    @DeleteMapping
    public Result dels(String ids){
        String[] split = ids.split(",");
        setmealService.dels(split);
        return Result.success();
    }

    /**
     * 回显修改套餐信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result info(@PathVariable("id") String id){
        SetmealVO vo = setmealService.info(Long.parseLong(id));
        return Result.success(vo);
    }

    /**
     * 修改套餐
     * @param dto
     * @return
     */
    @PutMapping
    public Result update(@RequestBody @Validated(UpdateGroup.class) SetmealDTO dto){
        setmealService.update(dto);
        return Result.success();
    }

    /**
     *     起售停售套餐
     * @param id
     * @param status
     * @return
     */
    @PostMapping("/status/{status}")
    public Result status(String id, @PathVariable("status") String status){
        setmealService.status(Long.parseLong(id), Integer.parseInt(status));
        return Result.success();
    }
}
